package cc.bukkitPlugin.banitem.command;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Map;

import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.util.BlockIterator;

import cc.bukkitPlugin.banitem.BanItem;
import cc.bukkitPlugin.banitem.CFG;
import cc.bukkitPlugin.banitem.util.BINMSUtil;
import cc.bukkitPlugin.util.JsonExtra;
import cc.bukkitPlugin.util.JsonExtra.Style;
import cc.bukkitPlugin.util.nbt.NBTUtil;
import cc.bukkitPlugin.util.plugin.command.TACommandBase;

public class CommandNBTDump extends TACommandBase<BanItem>{

    public CommandNBTDump(CommandExc pExector){
        super(pExector,"nbtdump");
    }

    @Override
    public boolean execute(CommandSender pSender,String pLabel,String[] pArgs){
        if(!pSender.hasPermission(this.mPluginName+"."+this.getCommandLabel()))
            return noPermission(pSender);
        if(!(pSender instanceof Player))
            return consoleNotAllow(pSender);
        Player tPlayer=(Player)pSender;
        if(pArgs.length>1)
            return errorArgsNumber(pSender,pArgs.length);
        if(pArgs.length==1){
            if(pArgs[0].equalsIgnoreCase("help"))
                return help(pSender,pLabel);
            else return unknowChildCommand(pSender,pLabel,pArgs[0]);
        }
        Map<String,Object> tContent;
        ItemStack tItem=tPlayer.getItemInHand();
        if(tItem!=null&&tItem.getType()!=Material.AIR){
            tContent=NBTUtil.getNBTTagMapFromItem(tItem);
            if(tContent==null||tContent.isEmpty())
                return send(pSender,"&c当前手上的物品没有NBT");
        }else{
            Block tblock=null;
            Iterator<Block> itr=new BlockIterator(tPlayer,8);
            while(itr.hasNext()){
                tblock=itr.next();
                if(tblock!=null&&tblock.getType()!=Material.AIR)
                    break;
            }
            if(tblock==null||tblock.getType()==Material.AIR)
                return send(pSender,"&c你必须在距离为8内盯着任意方块");
            tContent=BINMSUtil.getNBTTagMapFromBlock(tblock);
            if(tContent==null||tContent.isEmpty())
                return send(pSender,"&c当前注视着的方块没有NBT");
        }
        send(pSender,ChatColor.YELLOW+"================================");
        ArrayList<String> keyList=new ArrayList<>(tContent.keySet());
        Collections.sort(keyList,new Comparator<String>(){

            @Override
            public int compare(String pStr1,String pStr2){
                int result=pStr1.compareToIgnoreCase(pStr2);
                if(result!=0)
                    return result;
                return pStr1.compareTo(pStr2);
            }

        });
        String jsonPrefix=BanItem.getMsgPrefix()+"    "+CFG.cfg_color;
        for(String sKey : keyList){
            JsonExtra container=new JsonExtra(jsonPrefix,JsonExtra.Color.white);
            Object tValue=tContent.get(sKey);
            if(tValue==null)
                continue;
            if(BINMSUtil.clazz_NBTTagCompound.isInstance(tValue)||BINMSUtil.clazz_NBTTagList.isInstance(tValue)){
                container.addNormalWords("       ");
                JsonExtra nbtLabel=new JsonExtra(sKey,JsonExtra.Color.dark_gray);
                nbtLabel.setStyle(Style.strikethrough);
                nbtLabel.setHoverEvent(JsonExtra.HoverAction.show_text,"该节点为容器");
                container.addExtra(nbtLabel);
            }else{
                String strValue=NBTUtil.getNBTSimpleJson(tValue);
                JsonExtra copy=new JsonExtra(CFG.link_color+"复制",JsonExtra.Color.blue);
                copy.setStyle(Style.bold,Style.underline);
                copy.setHoverEvent(JsonExtra.HoverAction.show_text,"点击复制命令到聊天栏");
                copy.setClickEvent(JsonExtra.ClickAction.suggest_command,"/banitem add t:ban nl:"+sKey+" nv:"+strValue);
                container.addExtra(copy);
                container.addNormalWords("  ");
                JsonExtra nbtLabel=new JsonExtra(CFG.cfg_color+sKey,JsonExtra.Color.light_purple);
                nbtLabel.setHoverEvent(JsonExtra.HoverAction.show_text,"值: "+strValue);
                container.addExtra(nbtLabel);
            }
            container.sendToPlayer(tPlayer);
        }
        send(pSender,ChatColor.YELLOW+"=============END================");
        return true;
    }

    @Override
    public ArrayList<String> getHelp(CommandSender pSender,String pLabel){
        ArrayList<String> list=new ArrayList<>();
        boolean op=pSender.hasPermission(this.mPluginName+".nbtdump");
        if(op){
            list.add(CFG.cmd_color+this.mMainCmdLabel+" nbtdump");
            list.add(CFG.help_color+"列出手上物品的NBT节点,如果手上没东西");
            list.add(CFG.help_color+"将会尝试列出鼠标所指方块的NBT节点");
        }
        return list;
    }
}
